<?php
namespace app\home\controller;
use think\Controller;
use think\Request;
use think\facade\Session;
use think\facade\Cookie;
use app\home\model\Article;
use app\home\model\Category;
use app\home\model\History;
use app\home\model\User;
use app\home\controller\Index2;
class Index extends Controller
{
    //用户注册
    public function register(Request $request)
    {
        if($request->isPost()){
            //接收
            $data = $request->post();
            $valiCms = new \app\home\validate\Cms;
            if(!$valiCms->check($data)){
                $this->error($valiCms->getError());
            }
            //处理
            $modelUser = new User;
            $modelUser->password = $data['password'];
            if($modelUser->allowField(true)->save($data)){
                $this->success('注册成功');
            }else{
                $this->error('注册失败');
            }
        }

        return $this->fetch();
    }

    //登录
    public function login(Request $request)
    {
        $errorCount = Cookie::get('error') ?? 0;
        if($request->isPost()){
            //接收
            $data=$request->post();
            //验证
            $valiLogin = new \app\home\validate\Login;
            if(!$valiLogin->check($data)){
                $this->error($valiLogin->getError());
            }
            if(Cookie::get('error')>=3){
                if(!captcha_check($data['captcha'])){
                    $errorCount++;
                    Cookie::set('error',$errorCount);
                    $this->error('验证码输入错误',"",Cookie::get('error'));
                };
            }
            $user = new User;
            //通过账号判断,有没有此用户
            $info = $user->where('nickname',$data['nickname'])->find();
            if($info){
                //验证密码是否一样
                if(password_verify($data['password'],$info['password'])){
                    if($info['true_false'] == 0) $this->error('此账号已被禁用');
                    Session::set('userinfo',$info);//登录成功,把用户信息存起来
                    Cookie::delete('error');
                    $this->success('登录成功');
                }else{
                    $errorCount++;
                    Cookie::set('error',$errorCount);
                    $this->error('密码输入有误,请从新输入密码',"", Cookie::get('error'));
                }
            }else{
                $errorCount++;
                Cookie::set('error',$errorCount);
                $this->error('账号不存在,请输入有效的账号',"", Cookie::get('error'));
            }
        }
        return $this->fetch();
    }

    public function index(Request $request)
    {
        $this->assign([
                //index页面展示文章
                'list'=>Article::paginate(3),
                //栏目展示
                'data'=>Index2::mainCategory(),
                //热门文章
                'hot'=>Index2::hot(),
                //浏览历史
                'history'=>Index2::history(),
        ]);

       return $this->fetch();
    }

    //栏目
    public function list(Request $request)
    {

        $this->assign([
            //所有主栏目
            'data'=>Index2::mainCategory(),
            //当前选择的主栏目id
            'id'=>Category::where('id',$request->get('cid'))->find(),
            //当前主栏目的所有子栏目
            'son'=>Index2::columnSon($request),
            //跟当前主栏目所有关联的文章
            'article'=>Index2::column($request),
            //热门文章
            'hot'=>Index2::hot(),
            //浏览历史
            'history'=>Index2::history(),
        ]);
        return $this->fetch();
    }

    //显示文章内容
    public function show(Request $request)
    {
        if(empty(Session::get('userinfo'))){//没登陆状态
            if(!Session::get('history')){
                $arr[$request->get('id')] = Article::get($request->get('id'))->title;
                Session::set('history',$arr);
            }else{
                $arr = Session::get('history');
                $arr[$request->get('id')] = Article::get($request->get('id'))->title;
                Session::set('history',$arr);
            }
        }else{
            //把用户浏览的所有文章id取出来
            $arrHistoryId =  History::where('user_id',Session::get('userinfo')['id'])->column('article_id');
            if(!in_array($request->get('id'),$arrHistoryId)){
                History::create([
                    'user_id' => Session::get('userinfo')['id'],
                    'article_id' => $request->get('id'),
                    'browsing_time' => date('Y-m-d H:i:s')
                ]);
            }else{  //浏览的记录存在就更新时间
                History::update(['article_id'=>$request->get('id'),  'browsing_time' => date('Y-m-d H:i:s')]);
            }
        }

        $this->assign([
            //热门文章
            'hot'=>Index2::hot(),
            //当前展示文章
            'list'=>Article::where('id',$request->get('id'))->find(),
            //上一页
            'up'=>Article::where('id','<',$request->get('id'))->order('create_time','desc')->find(),
            //下一页
            'xia'=>Article::where('id','>',$request->get('id'))->order('create_time','asc')->find(),
            //主栏目
            'data'=>Index2::mainCategory(),
            //跟当前主栏目所有关联的文章
            'article'=>Index2::column($request),
            //当前主栏目的所有子栏目
             'son'=>Index2::columnSon($request),
            //浏览历史
            'history'=>Index2::history(),
        ]);
        return $this->fetch();
    }



}
